﻿using System;
using System.Runtime.InteropServices;
using System.ComponentModel.Design;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using System.Windows;
using Rechy.RechyVSTool.Common;
using EnvDTE;
using EnvDTE80;
using System.Collections.Generic;

namespace Rechy.RechyVSTool
{
    [PackageRegistration(UseManagedResourcesOnly = true)]
    [InstalledProductRegistration("#110", "#112", "1.0.2.0", IconResourceID = 400)]
    [ProvideMenuResource("Menus.ctmenu", 1)]
    [ProvideToolWindow(typeof(RechyVSToolWindow))]
    [Guid(GuidList.guidRechyVSToolPkgString)]

    //选项菜单中添加选项页
    [ProvideOptionPage(typeof(SettingPage),
                       "RVST",
                       "General",
                       0,
                       0,
                       true)]
    public sealed class RechyVSToolPackage : Package
    {
        public DTE2 dte = null;
        public PkgCmdIDList selectedMenu = PkgCmdIDList.Null;
        public List<string> lstAlreadyOpenFiles = new List<string>();
        public List<string> lstExcludeEndsWith = null;
        public OutputWindowPane myOutPane = null;
        public int count;
        private CmdRightMenu cmdRM = null;

        public RechyVSToolPackage()
        {
        }
        protected override void Initialize()
        {
            base.Initialize();

            dte = (DTE2)base.GetService(typeof(DTE));
            myOutPane = dte.ToolWindows.OutputWindow.OutputWindowPanes.Add("RVST");
            cmdRM = new CmdRightMenu(this);

            var mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
            if(null != mcs)
            {
                foreach (int item in Enum.GetValues(typeof(PkgCmdIDList)))
                {
                    var menuCommandID = new CommandID(GuidList.guidRechyVSToolCmdSet, item);
                    var menuItem = new MenuCommand(RightMenuItemsCommand, menuCommandID);
                    mcs.AddCommand(menuItem);
                }
            }
        }

        public void WriteLog(string log)
        {
            dte.StatusBar.Text = log;
            myOutPane.OutputString(log + "\r\n");
            count++;
        }

        private void ShowToolWindow(object sender, EventArgs e)
        {
            ToolWindowPane window = this.FindToolWindow(typeof(RechyVSToolWindow), 0, true);
            if((null == window) || (null == window.Frame))
            {
                throw new NotSupportedException(Resources.CanNotCreateWindow);
            }
            IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
            Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowFrame.Show());
        }

        private void MenuItemsCommand(object sender, EventArgs e)
        {
            /*
            string res = string.Empty;
            IVsUIShell uiShell = GetService(typeof(SVsUIShell)) as IVsUIShell;

            //列出工具窗口信息
            res = Test.Window.ListToolFrames(uiShell);
            MessageBox.Show(res);

            //列出文档窗口信息
            res = Test.Window.ListDocFrames(uiShell);
            MessageBox.Show(res);
             * */
        }

        private void RightMenuItemsCommand(object sender, EventArgs e)
        {
            if(null != cmdRM)
            {
                cmdRM.Run(sender, e);
            }
        }
    }
}
